package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.otaliastudios.cameraview.BitmapCallback;
import com.otaliastudios.cameraview.CameraException;
import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraOptions;
import com.otaliastudios.cameraview.PictureResult;
import com.otaliastudios.cameraview.VideoResult;
import com.otaliastudios.cameraview.controls.Audio;
import com.otaliastudios.cameraview.controls.Facing;
import com.otaliastudios.cameraview.controls.Mode;
import com.otaliastudios.cameraview.size.SizeSelector;
import com.otaliastudios.cameraview.size.SizeSelectors;

import java.io.File;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityShootBinding;
import stark.common.basic.utils.DensityUtil;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.basic.utils.WorkPathUtil;

public class ShootActivity extends BaseAc<ActivityShootBinding> {


    @Override
    protected int onCreate() {
        return R.layout.activity_shoot;
    }

    @Override
    protected void initView() {
        mDataBinding.ivShootBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivReversal.setOnClickListener(this);
        mDataBinding.ivStart.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivReversal://反转镜头
                reversalLens();
                break;
            case R.id.ivStart://开始拍摄
                startShot();
                break;
        }
    }

    @Override
    protected void initData() {
        getPermission();
    }

    private void getPermission() {
        StkPermissionHelper.permission(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO).reqPermissionDesc(getString(R.string.get_shoot_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                initCameraView();
            }
        }).request();
    }

    //初始化相机
    private void initCameraView() {
        mDataBinding.cameraView.setMode(Mode.VIDEO);
        mDataBinding.cameraView.setAudio(Audio.ON);
        mDataBinding.cameraView.setLifecycleOwner(this);
        int width = DensityUtil.getWith(mContext);
        int height = DensityUtil.getHeight(mContext);
        SizeSelector maxAreaSelector = SizeSelectors.maxArea(width * height);
        SizeSelector equalWidthSelector = SizeSelectors.withFilter(size -> size.getWidth() == width);
        mDataBinding.cameraView.setPictureSize(SizeSelectors.and(maxAreaSelector, equalWidthSelector));
        mDataBinding.cameraView.addCameraListener(new CameraListener() {
            @Override
            public void onCameraOpened(@NonNull CameraOptions options) {

            }

            @Override
            public void onCameraClosed() {
            }

            @Override
            public void onCameraError(@NonNull CameraException exception) {
            }

            @Override
            public void onPictureTaken(@NonNull PictureResult result) {

            }

            @Override
            public void onVideoRecordingStart() {
                //开始拍摄视频
                startRecordTime();
            }

            @Override
            public void onVideoRecordingEnd() {
                //视频拍摄结束
                mDataBinding.ivStart.setImageResource(R.drawable.sp);
            }

            @Override
            public void onVideoTaken(@NonNull VideoResult result) {
                //保存到相册
                FileP2pUtil.copyPrivateVideoToPublic(mContext, result.getFile().getPath());
            }
        });
    }

    //开始拍摄
    private void startShot() {
        if (mDataBinding.cameraView.isTakingVideo()) {
            mDataBinding.cameraView.stopVideo();
        } else {
            String filePath = WorkPathUtil.generateVideoFilePath(null);
            if (filePath != null) {
                mDataBinding.cameraView.takeVideoSnapshot(new File(filePath));
            }
        }
    }

    //开始拍摄视频
    private void startRecordTime() {
        mDataBinding.ivStart.setImageResource(R.drawable.ks);
    }

    //反转镜头
    private void reversalLens() {
        Facing curFacing = mDataBinding.cameraView.getFacing();
        if (curFacing == Facing.BACK) {
            mDataBinding.cameraView.setFacing(Facing.FRONT);
        } else {
            mDataBinding.cameraView.setFacing(Facing.BACK);
        }
    }

}